<?php

namespace app\admin\controller;

use think\Db;
use think\Cache;

class Admin extends Base
{

    /**
     * 管理员登录
     */
    public function login()
    {
        $data = [];
        $where = [];
        $request = request();
        input('account') ? $where['account'] = input('account') : $this->err('缺少账号');
        input('password') ? $where['password'] = md5(input('password')) : $this->err('缺少密码');
        $data['lastLoginTime'] = createTime();
        $data['lastLoginIP'] = $request->ip();

        $field = 'password';
        $admin = model('admin')->field($field, true)->where($where)->find();
        $admin ? true : $this->err('账号或密码错误');
        $admin['status'] != 0 ? true : $this->err('该账号已被禁用！');


        // dump("u" . $admin['id']);exit;
        $token = md5($admin['id'] + time());
        Cache::set("u" . $admin['id'], $token, 60 * 60 * 24);
        $admin['token'] = $token;
        // session('token', $token);

        $res = model('admin')->editData($data, $admin['id']);

        $res ? $this->suc($admin) : $this->err('登录失败！');
    }

    /**
     * 查询管理员
     */
    public function getAdminDetail()
    {
        $where = [];

        input('uid') ? $where['id'] = input('uid') : $this->err('缺少编码！');

        $admin = model('admin')->field('password', true)->where($where)->find();

        $admin['rights'] = explode(',', $admin['rights']);

        $admin ? $this->suc($admin) : $this->err('查询失败！');

    }

    /**
     * 修改管理员
     */
    public function editAdmin(){

        $data = [];
        input('uid') ? $data['id'] = input('uid') : $this->err('缺少编码！');
        input('name') ? $data['name'] = input('name') : $this->err('缺少姓名！');
        input('mobile') ? $data['mobile'] = input('mobile') : $this->err('缺少手机号！');
        input('status') != null ? $data['status'] = input('status') : $this->err('缺少状态！');

        //缺少权限
        input('rights/a') ? $data['rights'] = input('rights/a') : true;

        $data['rights'] = implode(',', $data['rights']);

        $res = model('admin')->editData($data, $data['id']);

        $res ? $this->suc($res) : $this->err('修改失败！');

    }


    /**
     * 获取管理员列表
     */

    public function getAdminList()
    {
        $where = [];

        $admin = model('admin')->field('password', true)->where($where)->paginate(10)->toArray();

        $res = $admin['data'];

        foreach ($res as &$value) {

            $value['rights'] = explode(',', $value['rights']);

            if ($value['rights'] == [""]) {

                $value['rights'] = [];
            }

        }

        $admin['data'] = $res;

        $admin ? $this->suc($admin) : $this->err('查询失败！');

    }


    /**
     * 添加管理员
     */

    public function addAdmin()
    {

        $data = [];

        input('name') ? $data['name'] = input('name') : $this->err('缺少姓名！');

        input('mobile') ? $data['mobile'] = input('mobile') : $this->err('缺少手机号！');

        input('account') ? $data['account'] = input('account') : $this->err('缺少账号');

        input('password') ? $data['password'] = md5(input('password')) : $this->err('缺少密码');

        //缺少权限
        input('rights/a') ? $data['rights'] = input('rights/a') : true;

        $data['rights'] = implode(',', $data['rights']);

        $data['createTime'] = createTime();

        $res = model('admin')->editData($data);

        $res ? $this->suc() : $this->err();

    }


    /**
     * 修改密码
     */
    public function editPwd()
    {
        $data = [];
        $where = [];
        input('uid') ? $where['id'] = input('uid') : $this->err('缺少编码！');
        input('oldPwd') ? $data['oldPwd'] = md5(input('oldPwd')) : $this->err('缺少原密码！');
        input('newPwd') ? $data['password'] = md5(input('newPwd')) : $this->err('缺少新密码！');

        $admin = model('admin')->field('password')->where($where)->find();

        $data['oldPwd'] == $admin['password'] ? true : $this->err('原密码错误！');

        $res = model('admin')->editData($data, $where['id']);

        $res ? $this->suc($res) : $this->err('修改失败！');

    }


    /**
     * 重置密码
     */

    public function editPassword()
    {

        $data = [];

        input('uid') ? $data['id'] = input('uid') : $this->err('缺少编码！');

        input('password') ? $data['password'] = md5(input('password')) : $this->err('缺少新密码！');

        $admin = model('admin')->editData($data, $data['id']);

        $admin ? $this->suc($admin) : $this->err('主键错误！');

    }

    /**
     * 删除管理员
     */

    public function delAdmin()
    {

        $where = [];

        input('uid') ? $where['id'] = input('uid') : $this->err('缺少编码！');

        $res = model('admin')->where($where)->delete();

        $res ? $this->suc($res) : $this->err('主键错误！');

    }

    /**
     * 管理员添加备注
     */

    public function editRecord()
    {

        $data = [];

        input('cid') ? $data['cid'] = input('cid') : $this->err('缺少用户主键');

        input('adminId') ? $data['adminId'] = input('adminId') : $this->err('缺少管理员主键！');

        input('record') ? $data['record'] = input('record') : $this->err('缺少记录内容！');

        input('applyId') ? $data['applyId'] = input('applyId') : true;

        $data['createTime'] = createTime();

        //contact 1-已联系 2-未联系

        $userData = [
            'id' => input('cid'),
            'contact' => 1
        ];

        // 启动事务
        Db::startTrans();
        try {

            $res = model('user_record')->editData($data);

            if(!empty($data['applyId'])){

                $count = model('user_record')->where(['applyId'=>$data['applyId'] ])->count();

            }else{

                $count = model('user_record')->where(['cid'=>$data['cid'] ])->count();
            }


            if($count==1){

                model('user')->editData($userData, $userData['id']);

            }

            // 提交事务
            Db::commit();

            $this->suc($res);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            $this->err('操作失败！');
        }


    }

    /**
     * 获取管理员备注列表
     */

    public function getRecordList()
    {

        $where = [];

        input('cid') ? $where['cid'] = input('cid') : true;

        input('applyId') ? $where['applyId'] = input('applyId') : true;

        if(empty($where['cid'])){

            $res = model('user_record')
                ->alias('ur')
                ->field('ur.*,a.name')
                ->join('admin a', 'a.id = ur.adminId', 'left')
                ->where($where)
                ->order('ur.id desc')
                ->select()
            ;

        }else{

            $where['applyId'] = 0;

            $res = model('user_record')
                ->alias('ur')
                ->field('ur.*,a.name')
                ->join('admin a', 'a.id = ur.adminId', 'left')
                ->where($where)
                ->order('ur.id desc')
                ->select()
            ;
        }

        $res ? $this->suc($res) : $this->err('没有记录！');

    }

}
